package xyz.hubery.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.Account;
import xyz.hubery.domain.ExchangeTrade;
import xyz.hubery.dto.FreezeDTO;
import xyz.hubery.dto.LockUserAmountDTO;
import xyz.hubery.dto.UpdateAccountDTO;
import xyz.hubery.feign.AccountServiceFeign;
import xyz.hubery.model.R;
import xyz.hubery.service.AccountService;
import xyz.hubery.vo.SymbolAssetVo;
import xyz.hubery.vo.UserTotalAccountVo;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/account")
@Api("账户管理")
public class AccountController implements AccountServiceFeign {

    @Autowired
    private AccountService accountService;


    @GetMapping("/{coinName}")
    @ApiOperation("获取当前用户的账户信息")
    public R<Account> getAccount(@PathVariable("coinName") String coinName) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Account account = accountService.getUserAccountByCoinName(userId, coinName);
        return R.ok(account);
    }


    @GetMapping("/total")
    @ApiOperation(value = "计算用户的总资产")
    public R<UserTotalAccountVo> total() {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        UserTotalAccountVo userTotalAccountVo = accountService.getUserTotalAccount(userId);
        return R.ok(userTotalAccountVo);
    }


    @GetMapping("/asset/{symbol}")
    @ApiOperation("查询用户某个交易对的资产")
    public R<SymbolAssetVo> getUserAssetBySymbol(@PathVariable String symbol) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        SymbolAssetVo symbolAssetVo = accountService.getUserAssetBySymbol(userId, symbol);
        return R.ok(symbolAssetVo);
    }

    @PostMapping("/lockUserAmount")
    @Override
    public R lockUserAmount(@RequestBody LockUserAmountDTO dto) {
        boolean ok = accountService.lockUserAmount(dto.getUserId(), dto.getCoinId(), dto.getMum(), dto.getType(), dto.getOrderId(), dto.getFee());
        return ok ? R.ok() : R.fail();
    }


    @PostMapping("/unFreeze")
    @Override
    public R unFreeze(@RequestBody FreezeDTO freezeDTO) {
        boolean ok = accountService.unFreeze(freezeDTO.getUserId(), freezeDTO.getCoinId(), freezeDTO.getFreeze());
        return ok ? R.ok() : R.fail();
    }

    /**
     * 更新账户余额
     */
    @PostMapping("/updateAccount")
    @Override
    public R updateAccountByExchangeTrade(@RequestBody UpdateAccountDTO accountDTO) {
        boolean ok = accountService.transferAmount(accountDTO);
        return ok ? R.ok() : R.fail();
    }


}
